package com.ahmedmisbah.andgenplus.utils.string;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sf.json.util.JSONUtils;

public class StringUtil {

	/**
	 * Checks if a string matches pattern in REGEX or not
	 * 
	 * @param regex
	 * @param inputStr
	 * @return true if string matches REGEX
	 */

	public static final String PACKAGE_PATTERN = "^(?:(?:((?![0-9_])[a-zA-Z0-9_]+)\\.?)+)(?<!\\.)$";
	public static final String JAVA_METHOD_VARIABLES_NAMING_PATTERN = "^[a-z][a-z0-9]*([A-Z][a-z0-9]+|[A-Z]$)*$";
	public static final String JAVA_TYPES_PATTERN = "^[A-Z][a-z0-9]+([A-Z][a-z0-9]+|[A-Z]$)*$";
	public static final String URL_PATTERN = "^(http|https|HTTP|HTTPS)\\://[a-zA-Z0-9\\-\\.]+(\\.[a-zA-Z]{2,3})?(\\:[0-9]+)?(/\\S*)?$";
	public static final String BOOLEAN_PATTERN="true|TRUE|FALSE|false";
	public static final String INTEGER_PATTERN="[0-9]+";
	public static final String FLOAT_PATTERN="[0-9]+\\.[0-9]+";
	public static final String LONG_PATTERN="[0-9]+l|L";
	public static final String DOUBLE_PATTERN="[0-9]+\\.[0-9]+d|D";
	
	
	public static boolean matchPattern(String regex, String inputStr) {
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(inputStr);
		return matcher.matches();
	}
	
	public static boolean isValidJson(String jsonString) {
		return JSONUtils.mayBeJSON(jsonString);
	}

	public static void main(String args[]) {
		//String testString = "{\"msg\":[{\"value\":33,\"label\":\"هواتف محمولة\"},{\"value\":45,\"label\":\"اجهزة لوحية\"}]}";
		//if (StringUtil.isValidJson(testString)) {
		String testString = "All";
		if (StringUtil.matchPattern(JAVA_TYPES_PATTERN, testString)) {
			System.out.println(testString + " is valid");
		} else {
			System.err.println(testString + " is invalid");
		}
	}

}
